package MineGod;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;

import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;

public class GUIScoreMenu extends GUI{

		static Texture tex;
	
        String[][] topScores = new String[5][2];
        static int playerScore;
        static String playerName;
        static int lowestHighScore;
        int startY;
        
        public GUIScoreMenu(){
                startY = 100;
                buttons.add(new GUIButtonStartMenu());
                loadTopScores();
                addTopScoreLabels();
                if(MineGod.gameFinished == true){
                	GUILabelScore temp = new GUILabelScore(startY+64);
                	temp.distance = playerScore;
                	temp.name = playerName;
                	labels.add(new GUILabelYourScore(450));
                	labels.add(temp);
                }
        }
        
    	public static void loadTexture(){
    		try {
    			tex = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/scoreScreen.png"));
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
        
        private void addTopScoreLabels() {
                for (String[] temp: topScores){
                        GUILabelScore temp2 = new GUILabelScore(startY);
                        temp2.name = temp[0];
                        temp2.distance = Integer.parseInt(temp[1]);
                        labels.add(temp2);
                        startY+=64;
                }
                
        }
        
        public static void updateLHS(){
                try {
                        String[][] topScores = new String[5][2];
                          FileInputStream fstream = new FileInputStream("scores.txt");
                          DataInputStream in = new DataInputStream(fstream);
                          BufferedReader br = new BufferedReader(new InputStreamReader(in));
                          String strLine;
                          int i = 0;
                          while ((strLine = br.readLine()) != null && i<5)   {
                                  if (strLine != "")
                                  {
                                          String[] temp = strLine.split("\t");
                                          topScores[i][0]=temp[0];
                                          topScores[i][1]=temp[1];
                                          i++;
                                  }
                          }
                          while (i<5){
                                  topScores[i][0]="Empty";
                                  topScores[i][1]="0";
                                  i++;
                          }
                          in.close();
                          lowestHighScore = Integer.parseInt(topScores[4][1]);
                } catch (FileNotFoundException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                }
        }

        private void loadTopScores(){
                try {
                          FileInputStream fstream = new FileInputStream("scores.txt");
                          DataInputStream in = new DataInputStream(fstream);
                          BufferedReader br = new BufferedReader(new InputStreamReader(in));
                          String strLine;
                          int i = 0;
              boolean wrotePlayerScore = false;
                          while ((strLine = br.readLine()) != null && i<5)   {
                                  if (strLine != "")
                                  {
                                          String[] temp = strLine.split("\t");
                                          if (!wrotePlayerScore && Integer.parseInt(temp[1])<playerScore){
                                                  topScores[i][0]=playerName;
                                                  topScores[i][1]=playerScore+"";
                                                  i++;
                                                  wrotePlayerScore = true;
                                          }
                                          if (i<5){
                                                  topScores[i][0]=temp[0];
                                                  topScores[i][1]=temp[1];
                                          }
                                          
                                          i++;
                                  }
                          }
                          while (i<5){
                                  topScores[i][0]="Empty";
                                  topScores[i][1]="0";
                                  i++;
                          }
                          in.close();
                          lowestHighScore = Integer.parseInt(topScores[4][1]);
                       
              FileOutputStream out;
              PrintStream p;
              
              try
              {
                      out = new FileOutputStream("scores.txt");

                      p = new PrintStream( out );
                      for (int j = 0; j<5; j++){
                          p.println(topScores[j][0]+"\t"+topScores[j][1]);
                      }

                      p.close();
              }
              catch (Exception e)
              {
                      System.err.println ("Error writing to file");
              }
                        
                } catch (FileNotFoundException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                }
        }

        public void updateButtons(MineGod game) {
                
        }


        @Override
        public void updateLabels(MineGod game) {
                // TODO Auto-generated method stub
                
        }
        
    	public void render(){
    		tex.bind();
    		GL11.glBegin(GL11.GL_QUADS);
    			GL11.glNormal3d(0, 0, 1);
    			GL11.glTexCoord2d(0.0, 0.0);
    			GL11.glVertex3d(0, 0, zIndex+25);
    			GL11.glTexCoord2d(1.0, 0.0);
    			GL11.glVertex3d(MineGod.windowWidth, 0, zIndex+25);
    			GL11.glTexCoord2d(1.0, 1.0);
    			GL11.glVertex3d(MineGod.windowWidth, MineGod.windowHeight, zIndex+25);
    			GL11.glTexCoord2d(0.0, 1.0);
    			GL11.glVertex3d(0, MineGod.windowHeight, zIndex+25);
    		GL11.glEnd();
    		for(GUIButton b: buttons){
    			b.render(zIndex);
    		}
    		for(GUILabel l: labels){
    			l.render(zIndex);
    		}
    		
    		
    		
    	}

        
        
}